package com.dwxt.community.admin.service;

import com.dwxt.common.base.YesOrNo;
import com.dwxt.community.dao.CommunityMapper;
import com.dwxt.community.entity.Community;
import com.dwxt.community.entity.CommunityExample;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommunityServiceAdmin {

    @Autowired
    private CommunityMapper communityMapper;

    public List<Community> selectCommunityList() {
        CommunityExample example = new CommunityExample();
        return communityMapper.selectByExample(example);
    }

    public void createOrUpdateCommunity(Community community) {
        if (community.getId() == null) {
            communityMapper.insertSelective(community);
        } else {
            communityMapper.updateByPrimaryKeySelective(community);
        }
    }

    public List<Community> selectDemonstrationCommunityList(String street) {
        CommunityExample example = new CommunityExample();
        CommunityExample.Criteria criteria = example.createCriteria().andIsDemonstrationEqualTo(YesOrNo.YES);
        if (StringUtils.isNotBlank(street)) {
            criteria.andStreetEqualTo(street);
        }
        return communityMapper.selectByExample(example);
    }

    public Community selectById(Integer communityId) {
        return communityMapper.selectByPrimaryKey(communityId);
    }
}
